import pkg from 'mockjs'
const { Random } = pkg
const timeout = 1000

const List: {
  username: string
  password: string
  id: string
}[] = [
  {
    username: 'admin',
    password: 'admin',
    id: '1',
  },
]

export default [
  // 登录接口
  {
    url: '/mock/user/login',
    method: 'post',
    timeout,
    response: ({
      body,
      headers,
    }: {
      body: {
        username: string
        password: string
      }
      headers: any
    }) => {
      const data = body
      let hasUser = false
      for (const user of List) {
        if (user.username === data.username && user.password === data.password) {
          hasUser = true
          return {
            code: 0,
            data: {
              username: user.username,
              id: user.id,
              token: Random.string('lower', 16),
            },
          }
        }
      }
      if (!hasUser) {
        return {
          code: 500,
          message: headers.lang === 'en' ? 'Account or password error' : '账号或密码错误',
        }
      }
    },
  },
]
